package com.seata.stock.service.impl;

import com.seata.stock.dao.StockDao;
import com.seata.stock.pojo.Stock;
import com.seata.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @ClassName: StockServiceImpl
 * @Author:
 * @Date:
 * @Version: 1.0
 */
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockDao stockDao;
    @Override
    public Stock searchStock(Integer productId) {
        Stock stock = stockDao.searchStock(productId);
        return stock;
    }

    @Override
    public Integer updateStock(Stock stock) {
        Stock stock1 = searchStock(stock.getProductId());
        //判断是否库存充足
        if (stock.getCount() >= stock1.getCount() || stock1 == null) return 99; // 库存不足返回错误码99
        //更新库存
        int newCount = stock1.getCount() - stock.getCount();
        stock.setCount(newCount);
        return stockDao.updateStock(stock);
    }
}
